﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Timers;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using GG.Data;
using GG.Mail;

namespace GG.CallsListNotification
{
    partial class CallsListNotification : ServiceBase
    {
        EventLog _eventLogCL = new EventLog();
        public CallsListNotification()
        {
            InitializeComponent();
            if (!System.Diagnostics.EventLog.SourceExists("GGAgentCallsListNotificationService"))
                System.Diagnostics.EventLog.CreateEventSource("GGAgentCallsListNotificationService",
                                                                      "GG - Agent Calls List Notification Service");
            _eventLogCL.Source = "GGAgentCallsListNotificationService";
        }

        protected override void OnStart(string[] args)
        {
            // TODO: Add code here to start your service.
            timer1.Interval = 5000;
            timer1.Enabled = true;
        }

        protected override void OnStop()
        {
            timer1.Stop();
            // TODO: Add code here to perform any tear-down necessary to stop your service.
        }

        private void timer1_Elapsed(object sender, ElapsedEventArgs e)
        {
            timer1.Enabled = false;
            string[] _file = new string[1];
            string ExcelFilePath = string.Empty;
            string _emailTitle = "";
            string _emailBody = "";
            string _toemail = "";
            string _ccemail = "";
            string _bccemail = "";
            string _contactusemail = "";
            string FromDate = "";
            string ToDate = "";

            string _emailTitleAD = "";
            string _emailBodyAD = "";
            string _toemailAD = "";
            string _ccemailAD = "";
            string _bccemailAD = "";

            try
            {
                MyData mydata = new MyData();
                if (mydata.GetEventCount() == 0)
                {
                    DateTime _dtTime = GG.Global.General.GetUTCDateTime();
                    FromDate = _dtTime.ToString("yyyyMMdd");
                    ToDate = _dtTime.ToString("yyyyMMdd");

                    DateTime d = GG.Global.General.GetUTCDateTime();
                    string day = d.DayOfWeek.ToString().ToUpper();
                    string dayName = "";
                    string dayNameforUser = "";
                    DateTime _dtDate = GG.Global.General.GetUTCDateTime();

                    _emailTitle = "GG Agent Calls List @ " + _dtDate.ToString("dd/MM/yyyy") + "  (" + _dtDate.ToString(" hh tt") + ")";
                    _emailTitleAD = "GG Total Leads List @ " + _dtDate.ToString("dd/MM/yyyy") + "  (" + _dtDate.ToString(" hh tt") + ")";

                    #region NOT USING
                    if (day == "MONDAY")
                    {
                        dayName = " (Friday + Weekends)";
                    }
                    else if (day != "MONDAY")
                    {
                        dayName = " (Yesterday)";
                    }
                    #endregion

                    DateTime _dtFromDate = GG.Global.General.GetUTCDateTime();
                    DateTime _dtToDate = GG.Global.General.GetUTCDateTime();
                    if (d.DayOfWeek == DayOfWeek.Monday)
                    {
                        _dtFromDate = d.AddDays(-3);
                    }
                    else
                    {
                        _dtFromDate = d.AddDays(-1);
                    }
                    _dtToDate = d.AddDays(-1);
                    _dtFromDate = mydata.GetExceptEventDays(_dtFromDate);
                    if (_dtFromDate != null)
                    {
                        if (_dtDate.ToString("tt").ToUpper() == "AM")
                        {
                            dayNameforUser = "(" + _dtFromDate.DayOfWeek.ToString() + " 12:45 pm to Today 8:15 am)";
                        }
                        else if (_dtDate.ToString("tt").ToUpper() == "PM")
                        {
                            dayNameforUser = "(Today 8:15 am to 12:45 pm)";
                        }
                    }

                    _toemail = System.Configuration.ConfigurationSettings.AppSettings["toemail"].ToString();
                    _ccemail = System.Configuration.ConfigurationSettings.AppSettings["ccemail"].ToString();
                    _bccemail = System.Configuration.ConfigurationSettings.AppSettings["bccemail"].ToString();
                    _contactusemail = System.Configuration.ConfigurationSettings.AppSettings["contactusemail"].ToString();

                    _toemailAD = System.Configuration.ConfigurationSettings.AppSettings["toemailAD"].ToString();
                    _ccemailAD = System.Configuration.ConfigurationSettings.AppSettings["ccemailAD"].ToString();
                    _bccemailAD = System.Configuration.ConfigurationSettings.AppSettings["bccemailAD"].ToString();

                    Int32 NewGGCustomer = 0; Int32 NewTHCustomer = 0; Int32 NewENCustomer = 0; Int32 NewMobileCustomer = 0; Int32 NewPromoCustomer = 0;
                    Int32 CallBackOverDue = 0; Int32 CallBackToday = 0; Int32 AccumulatedCallBack = 0; Int32 CallDirect = 0;
                    Int32 CallMeRequest = 0; Int32 CallMeUncontacted = 0; Int32 FileCount = 0; Int32 NameCount = 0;
                    Int32 UNContacted = 0; Int32 TotalCallBackOverDue = 0; Int32 TotalCallBackToday = 0; Int32 TotalUCBF = 0;
                    Int32 TotalNewLeads = 0; Int32 TotalOldLeads = 0; Int32 TotalOldLeadsA = 0; Int32 TotalOldLeadsB = 0;
                    Int32 TotalUNContacted = 0; Int32 TotalPromotionLeads = 0; Int32 TotalCallMeLeads = 0; Int32 TotalCallMeNFLeads = 0;

                    DataSet _dsAgentLeads = new DataSet();
                    _dsAgentLeads = mydata.GetAgentLeads();

                    #region Total Call List Value Assign
                    if (_dsAgentLeads != null && _dsAgentLeads.Tables.Count > 0 && _dsAgentLeads.Tables[0].Rows.Count > 0)
                    {
                        FileCount = _dsAgentLeads.Tables[0].Rows.Count;

                        if (_dsAgentLeads.Tables[0].Rows[0]["NewTHCustomer"] != DBNull.Value && _dsAgentLeads.Tables[0].Rows[0]["NewTHCustomer"].ToString() != string.Empty)
                        {
                            NewTHCustomer = Convert.ToInt32(_dsAgentLeads.Tables[0].Rows[0]["NewTHCustomer"].ToString());
                        }
                        if (_dsAgentLeads.Tables[0].Rows[0]["NewENCustomer"] != DBNull.Value && _dsAgentLeads.Tables[0].Rows[0]["NewENCustomer"].ToString() != string.Empty)
                        {
                            NewENCustomer = Convert.ToInt32(_dsAgentLeads.Tables[0].Rows[0]["NewENCustomer"].ToString());
                        }
                        if (_dsAgentLeads.Tables[0].Rows[0]["NewMobileCustomer"] != DBNull.Value && _dsAgentLeads.Tables[0].Rows[0]["NewMobileCustomer"].ToString() != string.Empty)
                        {
                            NewMobileCustomer = Convert.ToInt32(_dsAgentLeads.Tables[0].Rows[0]["NewMobileCustomer"].ToString());
                        }
                        if (_dsAgentLeads.Tables[0].Rows[0]["NewPromoCustomer"] != DBNull.Value && _dsAgentLeads.Tables[0].Rows[0]["NewPromoCustomer"].ToString() != string.Empty)
                        {
                            NewPromoCustomer = Convert.ToInt32(_dsAgentLeads.Tables[0].Rows[0]["NewPromoCustomer"].ToString());
                        }
                        if (_dsAgentLeads.Tables[0].Rows[0]["CallBackOverDue"] != DBNull.Value && _dsAgentLeads.Tables[0].Rows[0]["CallBackOverDue"].ToString() != string.Empty)
                        {
                            CallBackOverDue = Convert.ToInt32(_dsAgentLeads.Tables[0].Rows[0]["CallBackOverDue"].ToString());
                        }
                        if (_dsAgentLeads.Tables[0].Rows[0]["CallBackToday"] != DBNull.Value && _dsAgentLeads.Tables[0].Rows[0]["CallBackToday"].ToString() != string.Empty)
                        {
                            CallBackToday = Convert.ToInt32(_dsAgentLeads.Tables[0].Rows[0]["CallBackToday"].ToString());
                        }
                        if (_dsAgentLeads.Tables[0].Rows[0]["AccumulatedCallBack"] != DBNull.Value && _dsAgentLeads.Tables[0].Rows[0]["AccumulatedCallBack"].ToString() != string.Empty)
                        {
                            AccumulatedCallBack = Convert.ToInt32(_dsAgentLeads.Tables[0].Rows[0]["AccumulatedCallBack"].ToString());
                        }
                        if (_dsAgentLeads.Tables[0].Rows[0]["CallDirect"] != DBNull.Value && _dsAgentLeads.Tables[0].Rows[0]["CallDirect"].ToString() != string.Empty)
                        {
                            CallDirect = Convert.ToInt32(_dsAgentLeads.Tables[0].Rows[0]["CallDirect"].ToString());
                        }
                        if (_dsAgentLeads.Tables[0].Rows[0]["CallMeRequest"] != DBNull.Value && _dsAgentLeads.Tables[0].Rows[0]["CallMeRequest"].ToString() != string.Empty)
                        {
                            CallMeRequest = Convert.ToInt32(_dsAgentLeads.Tables[0].Rows[0]["CallMeRequest"].ToString());
                        }
                        if (_dsAgentLeads.Tables[0].Rows[0]["CallMeUncontacted"] != DBNull.Value && _dsAgentLeads.Tables[0].Rows[0]["CallMeUncontacted"].ToString() != string.Empty)
                        {
                            CallMeUncontacted = Convert.ToInt32(_dsAgentLeads.Tables[0].Rows[0]["CallMeUncontacted"].ToString());
                        }
                        if (_dsAgentLeads.Tables[0].Rows[0]["UNContacted"] != DBNull.Value && _dsAgentLeads.Tables[0].Rows[0]["UNContacted"].ToString() != string.Empty)
                        {
                            UNContacted = Convert.ToInt32(_dsAgentLeads.Tables[0].Rows[0]["UNContacted"].ToString());
                        }

                        NewGGCustomer = (NewTHCustomer + NewENCustomer + NewMobileCustomer + NewPromoCustomer);
                    }
                    #endregion

                    string strEmpty = ".";
                    if (FileCount > 0)
                    {
                        #region LEADS LIST
                        _emailBody += "<html>";
                        _emailBody += "<head>";
                        _emailBody += "<title></title>";
                        _emailBody += "</head>";
                        _emailBody += "<body class='email_body'>";

                        _emailBody += "<style type='text/css'>";
                        _emailBody += ".email_body {background: white;margin: 0;padding: 0;color: #4c4c4c;font-family: Arial, Helvetica, sans-serif;font-size: 0.85em;}";
                        _emailBody += "a {text-decoration:underline;color:#2c95cc;} ";
                        _emailBody += "#email_container {margin:0 auto;padding:20px;width:600px; height:auto;} ";
                        _emailBody += "#email_body {width:100%; text-align: left !important;} ";
                        _emailBody += ".email_pad10px {padding:10px;} ";
                        _emailBody += ".email_results td {width:33%;} ";
                        _emailBody += "</style>";

                        _emailBody += "<table width='75%' border='0' cellspacing='2' cellpadding='0' class='email_body'>";

                        _emailBody += "<tr><td style='color:#DF7401; font-size:large;'><b>GG Agent Calls List at " + _dtDate.ToString("dd/MMM/yyyy dddd") + "  (" + _dtDate.ToString(" hh tt") + ")</b></td></tr>";

                        #region Agent Call List
                        if (_dsAgentLeads.Tables.Count > 2 && _dsAgentLeads.Tables[2].Rows.Count > 0)
                        {
                            if (_dsAgentLeads.Tables.Count > 3 && _dsAgentLeads.Tables[3].Rows.Count > 0)
                            {
                                if (_dsAgentLeads.Tables[3].Rows[0]["TotalCallBackOverDue"] != DBNull.Value && _dsAgentLeads.Tables[3].Rows[0]["TotalCallBackOverDue"].ToString() != string.Empty)
                                {
                                    TotalCallBackOverDue = Convert.ToInt32(_dsAgentLeads.Tables[3].Rows[0]["TotalCallBackOverDue"].ToString());
                                }
                                if (_dsAgentLeads.Tables[3].Rows[0]["TotalCallBackToday"] != DBNull.Value && _dsAgentLeads.Tables[3].Rows[0]["TotalCallBackToday"].ToString() != string.Empty)
                                {
                                    TotalCallBackToday = Convert.ToInt32(_dsAgentLeads.Tables[3].Rows[0]["TotalCallBackToday"].ToString());
                                }
                                if (_dsAgentLeads.Tables[3].Rows[0]["TotalUCBF"] != DBNull.Value && _dsAgentLeads.Tables[3].Rows[0]["TotalUCBF"].ToString() != string.Empty)
                                {
                                    TotalUCBF = Convert.ToInt32(_dsAgentLeads.Tables[3].Rows[0]["TotalUCBF"].ToString());
                                }
                                if (_dsAgentLeads.Tables[3].Rows[0]["TotalNewLeads"] != DBNull.Value && _dsAgentLeads.Tables[3].Rows[0]["TotalNewLeads"].ToString() != string.Empty)
                                {
                                    TotalNewLeads = Convert.ToInt32(_dsAgentLeads.Tables[3].Rows[0]["TotalNewLeads"].ToString());
                                }
                                if (_dsAgentLeads.Tables[3].Rows[0]["TotalOldLeads"] != DBNull.Value && _dsAgentLeads.Tables[3].Rows[0]["TotalOldLeads"].ToString() != string.Empty)
                                {
                                    TotalOldLeads = Convert.ToInt32(_dsAgentLeads.Tables[3].Rows[0]["TotalOldLeads"].ToString());
                                }
                                if (_dsAgentLeads.Tables[3].Rows[0]["TotalOldLeadsA"] != DBNull.Value && _dsAgentLeads.Tables[3].Rows[0]["TotalOldLeadsA"].ToString() != string.Empty)
                                {
                                    TotalOldLeadsA = Convert.ToInt32(_dsAgentLeads.Tables[3].Rows[0]["TotalOldLeadsA"].ToString());
                                }
                                if (_dsAgentLeads.Tables[3].Rows[0]["TotalOldLeadsB"] != DBNull.Value && _dsAgentLeads.Tables[3].Rows[0]["TotalOldLeadsB"].ToString() != string.Empty)
                                {
                                    TotalOldLeadsB = Convert.ToInt32(_dsAgentLeads.Tables[3].Rows[0]["TotalOldLeadsB"].ToString());
                                }
                                if (_dsAgentLeads.Tables[3].Rows[0]["TotalUNContacted"] != DBNull.Value && _dsAgentLeads.Tables[3].Rows[0]["TotalUNContacted"].ToString() != string.Empty)
                                {
                                    TotalUNContacted = Convert.ToInt32(_dsAgentLeads.Tables[3].Rows[0]["TotalUNContacted"].ToString());
                                }
                                if (_dsAgentLeads.Tables[3].Rows[0]["TotalPromotionLeads"] != DBNull.Value && _dsAgentLeads.Tables[3].Rows[0]["TotalPromotionLeads"].ToString() != string.Empty)
                                {
                                    TotalPromotionLeads = Convert.ToInt32(_dsAgentLeads.Tables[3].Rows[0]["TotalPromotionLeads"].ToString());
                                }
                                if (_dsAgentLeads.Tables[3].Rows[0]["TotalCallMeLeads"] != DBNull.Value && _dsAgentLeads.Tables[3].Rows[0]["TotalCallMeLeads"].ToString() != string.Empty)
                                {
                                    TotalCallMeLeads = Convert.ToInt32(_dsAgentLeads.Tables[3].Rows[0]["TotalCallMeLeads"].ToString());
                                }
                                if (_dsAgentLeads.Tables[3].Rows[0]["TotalCallMeNFLeads"] != DBNull.Value && _dsAgentLeads.Tables[3].Rows[0]["TotalCallMeNFLeads"].ToString() != string.Empty)
                                {
                                    TotalCallMeNFLeads = Convert.ToInt32(_dsAgentLeads.Tables[3].Rows[0]["TotalCallMeNFLeads"].ToString());
                                }
                            }
                            Int16 _CurrentTime = 9;
                            DateTime _dtCurrentTime = GG.Global.General.GetUTCDateTime();
                            string _strCurrentTime = _dtCurrentTime.Hour.ToString("00");
                            if (_strCurrentTime != string.Empty)
                            {
                                if (!Int16.TryParse(_strCurrentTime, out _CurrentTime))
                                {
                                    _CurrentTime = 9;
                                }
                            }
                            Int32 Count = _dsAgentLeads.Tables[2].Rows.Count;
                            #region Assigned Leads List
                            _emailBody += "<tr><td>";
                            _emailBody += "<table width='100%' border='1' cellspacing='2' cellpadding='0' class='email_body'>";
                            _emailBody += "<tr>";
                            _emailBody += "<td style='width: 80px; height:30px; background: #045FB4; color:#ffffff;' align='center'><b>Agent</b></td>";
                            _emailBody += "<td style='width: 80px; height:30px; background: #045FB4; color:#ffffff;' align='center'><b>CallBack OverDue</b></td>";
                            //_emailBody += "<td style='width: 80px; height:30px; background: #045FB4; color:#ffffff;' align='center'><b>CallBack Today</b></td>";
                            _emailBody += "<td style='width: 100px; height:30px; background: #045FB4; color:#ffffff;' align='center'><b>UN-Contacted B/F</b></td>";
                            _emailBody += "<td style='width: 80px; height:30px; background: #045FB4; color:#ffffff;' align='center'><b>Promo Leads</b></td>";
                            _emailBody += "<td style='width: 80px; height:30px; background: #045FB4; color:#ffffff;' align='center'><b>Call Me Leads</b></td>";
                            _emailBody += "<td style='width: 80px; height:30px; background: #045FB4; color:#ffffff;' align='center'><b>Call Me Not Found Leads</b></td>";
                            _emailBody += "<td style='width: 80px; height:30px; background: #045FB4; color:#ffffff;' align='center'><b>New Leads</b></td>";
                            //_emailBody += "<td style='width: 80px; height:30px; background: #045FB4; color:#ffffff;' align='center'><b>Old Leads</b></td>";
                            _emailBody += "<td style='width: 80px; height:30px; background: #045FB4; color:#ffffff;' align='center'><b>Old Leads - A</b></td>";
                            _emailBody += "<td style='width: 80px; height:30px; background: #045FB4; color:#ffffff;' align='center'><b>Old Leads - B</b></td>";
                            _emailBody += "<td style='width: 80px; height:30px; background: #045FB4; color:#ffffff;' align='center'><b>Total Leads</b></td>";
                            _emailBody += "</tr>";

                            for (int ac = 0; ac < Count; ac++)
                            {
                                if (_dsAgentLeads.Tables[2].Rows[ac]["AgentType"] != DBNull.Value &&
                                    _dsAgentLeads.Tables[2].Rows[ac]["AgentType"].ToString() == "1")
                                {
                                    _emailBody += "<tr>";
                                    _emailBody += "<td style='height:30px; background: #fffe00;' align='center'>" + _dsAgentLeads.Tables[2].Rows[ac]["AgentName"].ToString() + "</td>";
                                    _emailBody += "<td style='height:30px; background: #fffe00;' align='center'>" + _dsAgentLeads.Tables[2].Rows[ac]["CallBackOverDue"].ToString() + "</td>";
                                    //_emailBody += "<td style='height:30px; background: #fffe00;' align='center'>" + _dsAgentLeads.Tables[2].Rows[ac]["CallBackToday"].ToString() + "</td>";
                                    _emailBody += "<td style='height:30px; background: #fffe00;' align='center'>" + _dsAgentLeads.Tables[2].Rows[ac]["UCBF"].ToString() + "</td>";
                                    _emailBody += "<td style='height:30px; background: #fffe00;' align='center'>" + _dsAgentLeads.Tables[2].Rows[ac]["PromotionLeads"].ToString() + "</td>";
                                    _emailBody += "<td style='height:30px; background: #fffe00;' align='center'>" + _dsAgentLeads.Tables[2].Rows[ac]["CallMeLeads"].ToString() + "</td>";
                                    _emailBody += "<td style='height:30px; background: #fffe00;' align='center'>" + _dsAgentLeads.Tables[2].Rows[ac]["CallMeNFLeads"].ToString() + "</td>";
                                    _emailBody += "<td style='height:30px; background: #fffe00;' align='center'>" + _dsAgentLeads.Tables[2].Rows[ac]["NewLeads"].ToString() + "</td>";
                                    //_emailBody += "<td style='height:30px; background: #fffe00;' align='center'>" + _dsAgentLeads.Tables[2].Rows[ac]["OldLeads"].ToString() + "</td>";
                                    _emailBody += "<td style='height:30px; background: #fffe00;' align='center'>" + _dsAgentLeads.Tables[2].Rows[ac]["OldLeadsA"].ToString() + "</td>";
                                    _emailBody += "<td style='height:30px; background: #fffe00;' align='center'>" + _dsAgentLeads.Tables[2].Rows[ac]["OldLeadsB"].ToString() + "</td>";
                                    _emailBody += "<td style='height:30px; background: #fffe00;' align='center'>" + _dsAgentLeads.Tables[2].Rows[ac]["TotalLeads"].ToString() + "</td>";
                                    _emailBody += "</tr>";
                                }
                                else if (_dsAgentLeads.Tables[2].Rows[ac]["AgentType"] != DBNull.Value &&
                                    _dsAgentLeads.Tables[2].Rows[ac]["AgentType"].ToString() == "2")
                                {
                                    _emailBody += "<tr>";
                                    _emailBody += "<td style='height:30px; background: #81DAF5;' align='center'>" + _dsAgentLeads.Tables[2].Rows[ac]["AgentName"].ToString() + "</td>";
                                    _emailBody += "<td style='height:30px; background: #81DAF5;' align='center'>" + _dsAgentLeads.Tables[2].Rows[ac]["CallBackOverDue"].ToString() + "</td>";
                                    //_emailBody += "<td style='height:30px; background: #81DAF5;' align='center'>" + _dsAgentLeads.Tables[2].Rows[ac]["CallBackToday"].ToString() + "</td>";
                                    _emailBody += "<td style='height:30px; background: #81DAF5;' align='center'>" + _dsAgentLeads.Tables[2].Rows[ac]["UCBF"].ToString() + "</td>";
                                    _emailBody += "<td style='height:30px; background: #81DAF5;' align='center'>" + _dsAgentLeads.Tables[2].Rows[ac]["PromotionLeads"].ToString() + "</td>";
                                    _emailBody += "<td style='height:30px; background: #81DAF5;' align='center'>" + _dsAgentLeads.Tables[2].Rows[ac]["CallMeLeads"].ToString() + "</td>";
                                    _emailBody += "<td style='height:30px; background: #81DAF5;' align='center'>" + _dsAgentLeads.Tables[2].Rows[ac]["CallMeNFLeads"].ToString() + "</td>";
                                    _emailBody += "<td style='height:30px; background: #81DAF5;' align='center'>" + _dsAgentLeads.Tables[2].Rows[ac]["NewLeads"].ToString() + "</td>";
                                    //_emailBody += "<td style='height:30px; background: #81DAF5;' align='center'>" + _dsAgentLeads.Tables[2].Rows[ac]["OldLeads"].ToString() + "</td>";
                                    _emailBody += "<td style='height:30px; background: #81DAF5;' align='center'>" + _dsAgentLeads.Tables[2].Rows[ac]["OldLeadsA"].ToString() + "</td>";
                                    _emailBody += "<td style='height:30px; background: #81DAF5;' align='center'>" + _dsAgentLeads.Tables[2].Rows[ac]["OldLeadsB"].ToString() + "</td>";
                                    _emailBody += "<td style='height:30px; background: #81DAF5;' align='center'>" + _dsAgentLeads.Tables[2].Rows[ac]["TotalLeads"].ToString() + "</td>";
                                    _emailBody += "</tr>";
                                }
                                else if (_dsAgentLeads.Tables[2].Rows[ac]["AgentType"] != DBNull.Value &&
                                    _dsAgentLeads.Tables[2].Rows[ac]["AgentType"].ToString() == "3")
                                {
                                    _emailBody += "<tr>";
                                    _emailBody += "<td style='height:30px; background: #FAAC58;' align='center'>" + _dsAgentLeads.Tables[2].Rows[ac]["AgentName"].ToString() + "</td>";
                                    _emailBody += "<td style='height:30px; background: #FAAC58;' align='center'>" + _dsAgentLeads.Tables[2].Rows[ac]["CallBackOverDue"].ToString() + "</td>";
                                    //_emailBody += "<td style='height:30px; background: #FAAC58;' align='center'>" + _dsAgentLeads.Tables[2].Rows[ac]["CallBackToday"].ToString() + "</td>";
                                    _emailBody += "<td style='height:30px; background: #FAAC58;' align='center'>" + _dsAgentLeads.Tables[2].Rows[ac]["UCBF"].ToString() + "</td>";
                                    _emailBody += "<td style='height:30px; background: #FAAC58;' align='center'>" + _dsAgentLeads.Tables[2].Rows[ac]["PromotionLeads"].ToString() + "</td>";
                                    _emailBody += "<td style='height:30px; background: #FAAC58;' align='center'>" + _dsAgentLeads.Tables[2].Rows[ac]["CallMeLeads"].ToString() + "</td>";
                                    _emailBody += "<td style='height:30px; background: #FAAC58;' align='center'>" + _dsAgentLeads.Tables[2].Rows[ac]["CallMeNFLeads"].ToString() + "</td>";
                                    _emailBody += "<td style='height:30px; background: #FAAC58;' align='center'>" + _dsAgentLeads.Tables[2].Rows[ac]["NewLeads"].ToString() + "</td>";
                                    //_emailBody += "<td style='height:30px; background: #FAAC58;' align='center'>" + _dsAgentLeads.Tables[2].Rows[ac]["OldLeads"].ToString() + "</td>";
                                    _emailBody += "<td style='height:30px; background: #FAAC58;' align='center'>" + _dsAgentLeads.Tables[2].Rows[ac]["OldLeadsA"].ToString() + "</td>";
                                    _emailBody += "<td style='height:30px; background: #FAAC58;' align='center'>" + _dsAgentLeads.Tables[2].Rows[ac]["OldLeadsB"].ToString() + "</td>";
                                    _emailBody += "<td style='height:30px; background: #FAAC58;' align='center'>" + _dsAgentLeads.Tables[2].Rows[ac]["TotalLeads"].ToString() + "</td>";
                                    _emailBody += "</tr>";
                                }
                            }

                            _emailBody += "<tr>";
                            _emailBody += "<td style='height:30px; background: #BDBDBD;' align='center'><b>TOTAL :</b></td>";
                            _emailBody += "<td style='height:30px; background: #BDBDBD;' align='center'><b>" + TotalCallBackOverDue.ToString() + "</b></td>";
                            //_emailBody += "<td style='height:30px; background: #BDBDBD;' align='center'><b>" + TotalCallBackToday.ToString() + "</b></td>";
                            _emailBody += "<td style='height:30px; background: #BDBDBD;' align='center'><b>" + TotalUCBF.ToString() + "</b></td>";
                            _emailBody += "<td style='height:30px; background: #BDBDBD;' align='center'><b>" + TotalPromotionLeads.ToString() + "</b></td>";
                            _emailBody += "<td style='height:30px; background: #BDBDBD;' align='center'><b>" + TotalCallMeLeads.ToString() + "</b></td>";
                            _emailBody += "<td style='height:30px; background: #BDBDBD;' align='center'><b>" + TotalCallMeNFLeads.ToString() + "</b></td>";
                            _emailBody += "<td style='height:30px; background: #BDBDBD;' align='center'><b>" + TotalNewLeads.ToString() + "</b></td>";
                            //_emailBody += "<td style='height:30px; background: #BDBDBD;' align='center'><b>" + TotalOldLeads.ToString() + "</b></td>";
                            _emailBody += "<td style='height:30px; background: #BDBDBD;' align='center'><b>" + TotalOldLeadsA.ToString() + "</b></td>";
                            _emailBody += "<td style='height:30px; background: #BDBDBD;' align='center'><b>" + TotalOldLeadsB.ToString() + "</b></td>";
                            _emailBody += "<td style='height:30px; background: #BDBDBD;' align='center'><b>" + TotalUNContacted.ToString() + "</b></td>";
                            _emailBody += "</tr>";

                            _emailBody += "</table>";
                            _emailBody += "</td></tr>";
                            #endregion
                        }
                        #endregion

                        _emailBody += "</table>";
                        _emailBody += "</body>";
                        _emailBody += "</html>";
                        #endregion

                        #region LEADS BANK
                        Int32 SNo = 0;
                        _emailBodyAD += "<html>";
                        _emailBodyAD += "<head>";
                        _emailBodyAD += "<title></title>";
                        _emailBodyAD += "</head>";
                        _emailBodyAD += "<body class='email_body'>";

                        _emailBodyAD += "<style type='text/css'>";
                        _emailBodyAD += ".email_body {background: white;margin: 0;padding: 0;color: #4c4c4c;font-family: Arial, Helvetica, sans-serif;font-size: 0.85em;}";
                        _emailBodyAD += "a {text-decoration:underline;color:#2c95cc;} ";
                        _emailBodyAD += "#email_container {margin:0 auto;padding:20px;width:600px; height:auto;} ";
                        _emailBodyAD += "#email_body {width:100%; text-align: left !important;} ";
                        _emailBodyAD += ".email_pad10px {padding:10px;} ";
                        _emailBodyAD += ".email_results td {width:33%;} ";
                        _emailBodyAD += "</style>";

                        _emailBodyAD += "<table width='75%' border='0' cellspacing='2' cellpadding='0' class='email_body'>";

                        _emailBodyAD += "<tr><td style='color:#DF7401; font-size:large;'><b>GG Total Leads List at " + _dtDate.ToString("dd/MMM/yyyy dddd") + "  (" + _dtDate.ToString(" hh tt") + ")</b></td></tr>";

                        #region Total Call List
                        _emailBodyAD += "<tr><td>&nbsp;</td></tr>";
                        _emailBodyAD += "<tr><td>&nbsp;</td></tr>";
                        _emailBodyAD += "<tr><td>";
                        _emailBodyAD += "<table width='100%' border='1' cellspacing='2' cellpadding='0' class='email_body'>";
                        _emailBodyAD += "<tr>";
                        _emailBodyAD += "<td style='width: 40px; height:30px; background: #045FB4; color:#ffffff;' align='center'><b>No.</b></td>";
                        _emailBodyAD += "<td style='width: 500px; height:30px; background: #045FB4; color:#ffffff;'><b>Description</b></td>";
                        _emailBodyAD += "<td style='width: 60px; height:30px; background: #045FB4; color:#ffffff;' align='center'><b>Total</b></td>";
                        _emailBodyAD += "</tr>";

                        SNo += 1;
                        _emailBodyAD += "<tr>";
                        _emailBodyAD += "<td style='height:30px; background: #fffe00;' align='center'>" + SNo.ToString() + "</td>";
                        _emailBodyAD += "<td style='height:30px; background: #fffe00;'>New TH Name Customer <span style='color:#DF0101; font-size: 12px; font-style:italic;'>" + dayNameforUser + "</span></td>";
                        _emailBodyAD += "<td style='height:30px; background: #fffe00;' align='center'>" + NewTHCustomer.ToString() + "</td>";
                        _emailBodyAD += "</tr>";

                        SNo += 1;
                        _emailBodyAD += "<tr>";
                        _emailBodyAD += "<td style='height:30px; background: #fffe00;' align='center'>" + SNo.ToString() + "</td>";
                        _emailBodyAD += "<td style='height:30px; background: #fffe00;'>New EN Name Customer <span style='color:#DF0101; font-size: 12px; font-style:italic;'>" + dayNameforUser + "</span></td>";
                        _emailBodyAD += "<td style='height:30px; background: #fffe00;' align='center'>" + NewENCustomer.ToString() + "</td>";
                        _emailBodyAD += "</tr>";

                        SNo += 1;
                        _emailBodyAD += "<tr>";
                        _emailBodyAD += "<td style='height:30px; background: #fffe00;' align='center'>" + SNo.ToString() + "</td>";
                        _emailBodyAD += "<td style='height:30px; background: #fffe00;'>New Mobile Customer <span style='color:#DF0101; font-size: 12px; font-style:italic;'>" + dayNameforUser + "</span></td>";
                        _emailBodyAD += "<td style='height:30px; background: #fffe00;' align='center'>" + NewMobileCustomer.ToString() + "</td>";
                        _emailBodyAD += "</tr>";

                        SNo += 1;
                        _emailBodyAD += "<tr>";
                        _emailBodyAD += "<td style='height:30px; background: #fffe00;' align='center'>" + SNo.ToString() + "</td>";
                        _emailBodyAD += "<td style='height:30px; background: #fffe00;'>New Promo Customer <span style='color:#DF0101; font-size: 12px; font-style:italic;'>" + dayNameforUser + "</span></td>";
                        _emailBodyAD += "<td style='height:30px; background: #fffe00;' align='center'>" + NewPromoCustomer.ToString() + "</td>";
                        _emailBodyAD += "</tr>";

                        _emailBodyAD += "<tr>";
                        _emailBodyAD += "<td style='height:30px; background: #99cdff;' align='center'></td>";
                        _emailBodyAD += "<td style='height:30px; background: #99cdff;'>Total New Customer GluayGluay.com <span style='color:#DF0101; font-size: 12px; font-style:italic;'>" + dayNameforUser + "</span></td>";
                        _emailBodyAD += "<td style='height:30px; background: #99cdff;' align='center'>" + NewGGCustomer.ToString() + "</td>";
                        _emailBodyAD += "</tr>";

                        _emailBodyAD += "<tr>";
                        _emailBodyAD += "<td style='height:30px;' colspan='3'>" + strEmpty.ToString() + "</td>";
                        _emailBodyAD += "</tr>";

                        SNo += 1;
                        _emailBodyAD += "<tr>";
                        _emailBodyAD += "<td style='height:30px; background: #fffe00;' align='center'>" + SNo.ToString() + "</td>";
                        _emailBodyAD += "<td style='height:30px; background: #fffe00;'>Call Direct <span style='color:#DF0101; font-size: 12px; font-style:italic;'>" + dayNameforUser + "</span></td>";
                        _emailBodyAD += "<td style='height:30px; background: #fffe00;' align='center'>" + CallDirect.ToString() + "</td>";
                        _emailBodyAD += "</tr>";

                        SNo += 1;
                        _emailBodyAD += "<tr>";
                        _emailBodyAD += "<td style='height:30px; background: #fffe00;' align='center'>" + SNo.ToString() + "</td>";
                        _emailBodyAD += "<td style='height:30px; background: #fffe00;'>Call Me Request <span style='color:#DF0101; font-size: 12px; font-style:italic;'>" + dayNameforUser + "</span></td>";
                        _emailBodyAD += "<td style='height:30px; background: #fffe00;' align='center'>" + CallMeRequest.ToString() + "</td>";
                        _emailBodyAD += "</tr>";

                        SNo += 1;
                        _emailBodyAD += "<tr>";
                        _emailBodyAD += "<td style='height:30px; background: #fffe00;' align='center'>" + SNo.ToString() + "</td>";
                        _emailBodyAD += "<td style='height:30px; background: #fffe00;'>Call Me Un-Contacted</td>";
                        _emailBodyAD += "<td style='height:30px; background: #fffe00;' align='center'>" + CallMeUncontacted.ToString() + "</td>";
                        _emailBodyAD += "</tr>";

                        _emailBodyAD += "<tr>";
                        _emailBodyAD += "<td style='height:30px;' colspan='3'>" + strEmpty.ToString() + "</td>";
                        _emailBodyAD += "</tr>";

                        SNo += 1;
                        _emailBodyAD += "<tr>";
                        _emailBodyAD += "<td style='height:30px; background: #fffe00;' align='center'>" + SNo.ToString() + "</td>";
                        _emailBodyAD += "<td style='height:30px; background: #fffe00;'>Accumulated Call Backs</td>";
                        _emailBodyAD += "<td style='height:30px; background: #fffe00;' align='center'>" + AccumulatedCallBack.ToString() + "</td>";
                        _emailBodyAD += "</tr>";

                        _emailBodyAD += "<tr>";
                        _emailBodyAD += "<td style='height:30px;' colspan='3'>" + strEmpty.ToString() + "</td>";
                        _emailBodyAD += "</tr>";

                        SNo += 1;
                        _emailBodyAD += "<tr>";
                        _emailBodyAD += "<td style='height:30px; background: #fffe00;' align='center'>" + SNo.ToString() + "</td>";
                        _emailBodyAD += "<td style='height:30px; background: #fffe00;'>Call Back Due Today</td>";
                        _emailBodyAD += "<td style='height:30px; background: #fffe00;' align='center'>" + CallBackToday.ToString() + "</td>";
                        _emailBodyAD += "</tr>";

                        SNo += 1;
                        _emailBodyAD += "<tr>";
                        _emailBodyAD += "<td style='height:30px; background: #fffe00;' align='center'>" + SNo.ToString() + "</td>";
                        _emailBodyAD += "<td style='height:30px; background: #fffe00;'>Total Un-Contacted Leads</td>";
                        _emailBodyAD += "<td style='height:30px; background: #fffe00;' align='center'>" + UNContacted.ToString() + "</td>";
                        _emailBodyAD += "</tr>";

                        _emailBodyAD += "</table>";
                        _emailBodyAD += "</td></tr>";
                        #endregion

                        _emailBodyAD += "</table>";
                        _emailBodyAD += "</body>";
                        _emailBodyAD += "</html>";
                        #endregion

                        string IsMailSend = ConfigurationManager.AppSettings["IsMailSend"].ToString();
                        if (IsMailSend.ToString() != string.Empty && IsMailSend == "1")
                        {
                            #region Email - Agent Call List
                            bool _emailSend = false;
                            while (!_emailSend)
                            {
                                if (MyMailManager.SendEmail(_emailTitle, _emailBody, _toemail, _ccemail, _bccemail))
                                {
                                    _emailSend = true;
                                    _eventLogCL.WriteEntry(
                                        "GG Agent Calls List Sent successfully [" +
                                        GG.Global.General.GetUTCDateTime().ToString() + "].", EventLogEntryType.SuccessAudit);
                                }
                                else
                                {
                                    timer1.Interval = 24 * 60 * 60 * 1000;
                                    System.Threading.Thread.Sleep(30 * 60 * 1000);
                                    _eventLogCL.WriteEntry(
                                        "GG Agent Calls List can't be Sent [" +
                                        GG.Global.General.GetUTCDateTime().ToString() + "].", EventLogEntryType.Error);
                                }
                            }
                            #endregion

                            #region Email - Total List
                            bool _emailSendAD = false;
                            while (!_emailSendAD)
                            {
                                if (MyMailManager.SendEmail(_emailTitleAD, _emailBodyAD, _toemailAD, _ccemailAD, _bccemailAD))
                                {
                                    _emailSendAD = true;
                                    _eventLogCL.WriteEntry(
                                        "GG Total Leads List Sent successfully [" +
                                        GG.Global.General.GetUTCDateTime().ToString() + "].", EventLogEntryType.SuccessAudit);
                                }
                                else
                                {
                                    timer1.Interval = 24 * 60 * 60 * 1000;
                                    System.Threading.Thread.Sleep(30 * 60 * 1000);
                                    _eventLogCL.WriteEntry(
                                        "GG Total Leads List can't be Sent [" +
                                        GG.Global.General.GetUTCDateTime().ToString() + "].", EventLogEntryType.Error);
                                }
                            }
                            #endregion
                        }
                        else
                        {
                            _eventLogCL.WriteEntry(
                                "GG Agent Calls List Mail Send Status Disabled.", EventLogEntryType.Information);
                        }
                    }
                    else
                    {
                        _eventLogCL.WriteEntry("No more Agent Calls Today [" + GG.Global.General.GetUTCDateTime().ToString() + "].", EventLogEntryType.Information);
                    }
                }
                else
                {
                    _eventLogCL.WriteEntry("Public Holiday [" + GG.Global.General.GetUTCDateTime().ToString() + "].", EventLogEntryType.Information);
                }
            }
            catch (Exception Ex)
            {
                _eventLogCL.WriteEntry("GG Agent Calls List Sending Error " + Ex.Message, EventLogEntryType.Error);
                CreateExceptionLog(Ex);
            }
        }

        private string GetExcelFilePath()
        {
            string _filePath = System.Configuration.ConfigurationSettings.AppSettings["FileOutputPath"].ToString();
            GG.InputOut.MyIO _myIO = new GG.InputOut.MyIO();
            string _currentMonth = GG.Global.General.GetUTCDateTime().ToString("yyyyMMM");
            string _todaydate = GG.Global.General.GetUTCDateTime().ToString("yyyyMMMdd");
            _filePath += "exceloutput\\englishname\\";
            if (!_myIO.CheckDirectory(_filePath + _currentMonth))
            {
                if (_myIO.CreateDirectory(_filePath, _currentMonth))
                {
                    _filePath += _currentMonth + "\\";
                }
            }
            else
            {
                _filePath += _currentMonth + "\\";
            }

            if (!_myIO.CheckDirectory(_filePath + _todaydate))
            {
                if (_myIO.CreateDirectory(_filePath, _todaydate))
                {
                    _filePath += _todaydate + "\\";
                }
            }
            else
            {
                _filePath += _todaydate + "\\";
            }
            return _filePath;
        }

        private static void CreateExceptionLog(Exception Ex)
        {
            String[] _fileContent = new String[6];
            _fileContent[0] = "Source : " + Ex.Source;
            _fileContent[1] = "Date Time : " + GG.Global.General.GetUTCDateTime().ToString();
            _fileContent[2] = "Stack Trace :" + Ex.StackTrace.ToString();
            _fileContent[3] = "Target Site : " + Ex.TargetSite.ToString();
            _fileContent[4] = "Location : " + "CallListNotification";
            _fileContent[5] = "Message : " + Ex.Message;

            WriteExceptionLog("Excel_", _fileContent);
        }

        private static void WriteExceptionLog(string strPrefixFileName, string[] strException)
        {
            try
            {
                GG.InputOut.MyIO _newFile = new GG.InputOut.MyIO();
                string _filePath = AppDomain.CurrentDomain.BaseDirectory + "Exception\\";
                if (_newFile.CheckDirectory(_filePath))
                {
                    string _curMonth = GG.Global.General.GetUTCDateTime().Year.ToString("0000") + GG.Global.General.GetUTCDateTime().Month.ToString("00");

                    if (!_newFile.CheckDirectory(_filePath + _curMonth))
                    {
                        if (_newFile.CreateDirectory(_filePath, _curMonth))
                        {
                            _filePath += _curMonth + "\\";
                        }
                    }
                    else
                    {
                        _filePath += _curMonth + "\\";
                    }

                    string _fileName = strPrefixFileName + _curMonth + GG.Global.General.GetUTCDateTime().Day.ToString("00") + GG.Global.General.GetUTCDateTime().Second.ToString("00") + GG.Global.General.GetUTCDateTime().Millisecond.ToString("00") + ".txt";

                    _newFile.WriteNewFile(_filePath, _fileName, strException);
                    _newFile = null;
                }
            }
            catch (Exception Ex)
            {
                Ex = null;
            }
        }
    }
}
